package cc.androidos;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cc.androidos.commons.GlobalParameter;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class LocalDictionary extends ListActivity {
	
    private   String[] sql = new String[] {"word", "TARGET_LANGURE"}; 
    
	private EditText dSerchText;
	private TextView dShowText;
	private TextView wordText;
	
	private ArrayList<Map<String, Object>> coll = new ArrayList<Map<String, Object>>();
	private ArrayList<Map<String, Object>> tempColl = new ArrayList<Map<String, Object>>();
	private Map<String, Object> item = new HashMap<String, Object>();
	private Map<String, Object> tempItem = new HashMap<String, Object>();
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        createTextView();
    }
    
    private void createTextView() {
    	setContentView(R.layout.dictionary);
    	
    	dSerchText = (EditText) findViewById(R.id.serchText);
    	dShowText = (TextView) findViewById(R.id.showText);	
    	wordText = (TextView) findViewById(R.id.wordText);	
    }
    
    
    @Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
    	if (keyCode >= 29 && keyCode <= 54 || keyCode == 62 || keyCode == 67) {
    		if (coll != null || item != null) {
        		coll.clear();
        		item.clear();
        		tempColl.clear();
        		tempItem.clear();
        	   	dShowText.setText("");	
            	wordText.setText("");		
        		
    		}
    		createListView(dSerchText.getText().toString()+"%");
    		Log.d("*********************************",String.valueOf(keyCode));
    	}
    		return super.onKeyUp(keyCode, event);
	}

	private void createListView(String text) {
        Intent intent = getIntent();
        
		if (intent.getData() == null) {
			intent.setData(GlobalParameter.CONTENT_URI);
		}
		
		sql =  new String[]{text};
		Cursor cur = getContentResolver().query(getIntent().getData(),
				sql, GlobalParameter.QUERY_TYPE_0, null, null);
		

		cur.moveToFirst();
		String showWord = "";
		int i= 0;
		while (!cur.isAfterLast()) {
			item = new HashMap<String, Object>();
			tempItem = new HashMap<String, Object>();
			showWord = cur.getString(0);
			if (showWord.length()<=9) {
				item.put("coolkey", cur.getString(0));
			} else {
				item.put("coolkey", cur.getString(0).substring(0,6)+"...");
			}
			tempItem.put("coolkey", showWord);
			coll.add(item);
			tempColl.add(i,tempItem);
			++i;
			cur.moveToNext();
		}
		
		this.setListAdapter(new SimpleAdapter(this, coll,
				android.R.layout.simple_list_item_1, new String[] {"coolkey" },
				new int[] { android.R.id.text1 })
		); 
    }
    
    private String onQuery(String text) {
    	Intent intent = getIntent();
        String result = "";
    	
		if (intent.getData() == null) {
			intent.setData(GlobalParameter.CONTENT_URI);
		}
		sql = new String[]{text};
		Cursor cur = getContentResolver().query(getIntent().getData(),
				sql,GlobalParameter.QUERY_TYPE_1, null, null);
		

		cur.moveToFirst();
		
		while (!cur.isAfterLast()) {
			result += cur.getString(0);
			cur.moveToNext();
		}
		return result;
    }
    
    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id){ 
    			dShowText.setText(onQuery(tempColl.get(position).get("coolkey").toString()));
    			wordText.setText(tempColl.get(position).get("coolkey").toString());
    			
    			//if (position)
    			

    			
    }
    
    
}